package com.neuedu.his.service;

import com.neuedu.his.dao.DepartmentDao;
import com.neuedu.his.entity.Department;
import com.neuedu.his.framework.BaseService;
import com.neuedu.his.framework.PageInfo;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * 项目：      his
 * 类名:       DepartmentService
 * 创建时间：  2020/12/23  8:52
 * 描述 :     用户测试类
 * 作者 :     张金山
 * QQ :     314649444
 * Site:      https://jshand.gitee.io
 */
public class DepartmentService implements BaseService<Department> {
    private DepartmentDao departmentDao = new DepartmentDao();


    @Override
    public List<Department> queryList(HttpServletRequest request) {
        String sql = " select * from department where active = 1 ";
        return departmentDao.selectList(sql);
    }


    /**
     * 分页查询
     * @param request
     * @return
     * @throws UnsupportedEncodingException
     */
    @Override
    public PageInfo queryListByPage(HttpServletRequest request) throws UnsupportedEncodingException {
        PageInfo pageInfo = getPageInfo(request);

        String name = request.getParameter("name");


        if(name == null ){
            name = "";
        }

        name = "%"+name.trim()+"%";


        String sql = " select * from department  where active = 1 and name like ?   limit ?  , ?  ";

        int start = (pageInfo.getPageNo()-1) * pageInfo.getPageSize() ;
        List<Department> list =  departmentDao.selectList(sql,name, start,pageInfo.getPageSize());

        String sql2 = "  select count(1) from department  where active = 1 and name like ?   ";
        long count =  departmentDao.selectCount(sql2,name);

        pageInfo.setList(list);
        pageInfo.setTotalCount((int) count);

        return pageInfo;
    }

    @Override
    public Department queryById(Integer id) {
        return departmentDao.selectById(id);
    }

    @Override
    public boolean edit(Department user) {
        return false;
    }


    /**
     * 逻辑删除，没有真正删除数据
     * @param id
     * @return
     */
    @Override
    public boolean deleteById(Integer id) {

        return  departmentDao.deleteById(id);
    }

    @Override
    public boolean save(Department user) {
        return departmentDao.insert(user);
    }

    @Override
    public boolean batchdel(String[] ids) throws Exception {
        return false;
    }

    public boolean update(Department user) {
        return departmentDao.update(user);
    }


}
